import mysql.connector
from sqlalchemy.orm import sessionmaker  # 创建会话
from sqlalchemy import create_engine  # 导入创建引擎


class ORM:
    # 会话的方法
    @classmethod
    def db(cls):
        mysql_configs = dict(
            db_host="127.0.0.1",  # 主机地址
            db_name="monitor",  # 数据库名称
            db_port="3306",
            db_user="root",
            db_passwor="root"
        )
        link = "mysql+mysqlconnector://{db_user}:{db_passwor}@{db_host}:{db_port}/{db_name}?charset=utf8".format(
            **mysql_configs
        )
        # 创建引擎
        engine = create_engine(
            link,
            encoding="utf-8",
            echo=False,
            pool_size=100,
            pool_recycle=10,
            connect_args={'charset': 'utf8'}
        )
        # 创建会话
        Session = sessionmaker(
            bind=engine,
            autocommit=False,
            autoflush=True,
            expire_on_commit=False
        )
        return Session()
